#include "XTcp.h"
#include "Logger.h"
#include "LogConsoleRecorder.h"
#include "LogFileRocorder.h"
#include <memory>
#include <thread>
#include "TestThread.h"

int main(int argc, char** argv)
{
	//初始化日志器
	std::string filename = "test.log";
	LOGGER.addLogRecorder(std::make_shared<LogFileRocorder>(filename));
	LOGGER.addLogRecorder(std::make_shared<LogConsoleRecorder>());
	LOGGER.start();

	unsigned short port = 8080;
	if (argc > 1)
	{
		port = atoi(argv[1]);
	}
	XTcp server;
	if (!server.bind(port))
	{
		INFOLOGGER("Bind failed");
		return -1;
	}

	while (true)
	{
		XTcp client = server.accept();//一个阻塞函数，未接受会阻塞，并非立刻返回值
		if (client.isConnected())
		{
			auto th = new TestThread;
			th->setSocket(client.getSock());
			std::thread thd(&TestThread::run, th);//启动线程
			thd.detach();//主线程子线程分离
		}
	}
	server.close();
	getchar();
	return 0;
}